package my.game.Descension;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;

public class Controller extends JPanel implements Runnable {

	private Thread game_thread;
	private Creature player;
	private Display disp;
	private LevelLoader levelLoader;
	
	//TODO: Add title menu options
	private boolean onOverworld;
	private boolean inTitleMenu; // For opening menu control
	private boolean inBattle; // For battle control
	
	final int FPS = 10; // frames per second.
	final int SKIP_TICKS = 1000/FPS;
	
	final int TILE_SIZE = 32;
	final int SCREEN_SIZE = TILE_SIZE * 20;
	
	public Controller() {
		setFocusable(true);
		//setBackground(Color.black);
		setSize(SCREEN_SIZE +12 , SCREEN_SIZE + 12);
		this.setLayout(null); // Absolute positioning of components.
		addKeyListener(new TAdapter());
		player = new Creature("player");
		disp = new Display();
		disp.setPlayer(player);
		this.add(disp);
		
		try {
		levelLoader = new LevelLoader("overworld");
		disp.setMap(levelLoader);
		player.setLevel(levelLoader);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		onOverworld = true; // Start of game in overworld
		player.setisOnOverworld(onOverworld);
	}
	
	public void addNotify() {
		super.addNotify();
		game_thread = new Thread(this);
		game_thread.start();
	}
	
	public void run() {
		long sleep;
		long next_game_tick;
		next_game_tick = System.currentTimeMillis();
		sleep = 0;
		
		
		while(true) {
			
			if (inTitleMenu) {
				//TODO: Create title menu things
			}
			
			// Game loop
			if (player.isOnOverworld()) {
				
				player.move();
				if (player.startBattle()) {
					onOverworld = false;
					inBattle = true;
					
				}
				if (player.pressedSelect() && player.isOnDoor()) {
					try {
					
					
						// Store the name of the level we were just in.
						String old = levelLoader.getCurrentLevelName();
						
						// Store coordinates to put player.
						int[] coords = levelLoader.doorDestination; 
						int[] coords2 = levelLoader.door2Destination;
						
						// Get our door.
						Sprite door = levelLoader.findSprite(player.getX(), player.getY());
						String newMap = door.newMap();
						
						// Load the new level.
						levelLoader = new LevelLoader(newMap);
						
						// Sync our levels across the classes.
						disp.setMap(levelLoader);
						player.setLevel(levelLoader);
					
						
						//System.out.println("Level loaded");
						//System.out.println("Old position: " + old);
						//System.out.println("Door: " + test.newLocation());
						// Find the location of the door in the new map and put the player there.
						
						if (door.isDoor1()) {
							player.getSprite().setX(coords[0]);
							player.getSprite().setY(coords[1]);
						}
						else {
							player.getSprite().setX(coords2[0]);
							player.getSprite().setY(coords2[1]);
						}
						
						//player.getSprite().setX(levelLoader.findSprite(player.getSprite().newLocation()).getX());
						//player.getSprite().setY(levelLoader.findSprite(player.getSprite().newLocation()).getY());
					
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			
				//disp.drawDoorLocation(levelLoader.findSprite(true));
				disp.repaint();
			}
			
			// While battle is afoot.
		
			if (player.isInBattle()) {
				
				player.move();
				disp.repaint();
			}
		
			
			
			
			
			// FPS code. Keeps everything chugging at a constant fps.
			next_game_tick += SKIP_TICKS;
			
			sleep = next_game_tick - System.currentTimeMillis();
			if(sleep >= 0)
				{
					try {
						Thread.sleep(sleep);
						} catch (InterruptedException e) {
							System.out.println("Interrupted");
						}
				}
			
		}
	}
	
	private class TAdapter extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			player.KeyPressed(e);
		}
		
		public void keyReleased(KeyEvent e) {
			player.KeyReleased(e);
		}
	}
	
}












/* In case shit goes bad.
	String old = levelLoader.findSprite(player.getX(), player.getY()).newLocation();
	levelLoader = new LevelLoader(levelLoader.findSprite(player.getX(), player.getY()).newLocation());
	disp.setMap(levelLoader);
	player.setLevel(levelLoader);

	Sprite test = levelLoader.findSprite(old);

	System.out.println("Level loaded");
	System.out.println("Old position: " + old);
	System.out.println("Door: " + test.newLocation());
	// Find the location of the door in the new map and put the player there.


	player.getSprite().setX(test.getX());
	player.getSprite().setY(test.getY());

	//player.getSprite().setX(levelLoader.findSprite(player.getSprite().newLocation()).getX());
	//player.getSprite().setY(levelLoader.findSprite(player.getSprite().newLocation()).getY());


 */


